﻿using FineUI;
using System;
using ZHDJ.BranchCenter.BLL;
using ZHDJ.Core;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models.Common;
using ZHDJ.WebPage;

namespace ZHDJ.BranchCenter.Module.Article
{
    public partial class PartyArticleManageEdit : PageBase
    {
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "PartyArticleAdd";
            }
        }

        public string ArticleModule
        {
            get
            {
                return this.Request["module"] ?? "";
            }
        }
        #endregion

        #region 页面属性
        public string EditorContent
        {
            get
            {
                string id = GetQueryIntValue("id");
                if (!string.IsNullOrEmpty(id))
                {
                    PartyArticle article = DB.PartyArticles.Find(id);
                    return article.Content;
                }
                else
                {
                    return string.Empty;
                }
            }
        }
        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }

        private void BindData()
        {
            //绑定下拉菜单
            ControlBusiness.ControlBindDataDictionary(ddlCategory, SysDataDictionary.ThreeAffairOpen, false, "");

            ControlBusiness.ControlBindDataDictionary(rblFlags, "ZHDJ.Article.Flags");
            rblFlags.SelectedIndex = 0;
        }

        private void LoadData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            string id = GetQueryIntValue("id");
            if (id != "")
            {
                PartyArticle article = DB.PartyArticles.Find(id);
                if (article == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                txtTitle.Text = article.Title;
                txtSubhead.Text = article.Subhead;
                txtAuthor.Text = article.Author;
                txtAuthorUrl.Text = article.AuthorUrl;
                txtArticlesLable.Text = article.ArticlesLable;
                rblFlags.SelectedValue = article.Flags.ToString();
                ddlCategory.SelectedValue = article.Category;
                txtSummary.Text = article.Summary;
                ppBranch.Value = article.BranchID;
                ppBranch.Text = ControlBusiness.GetBranchPickerText(article.BranchID);
                hdImageId.Text = article.ImageID;
                if (!string.IsNullOrEmpty(hdImageId.Text))
                {
                    filePhoto.EmptyText = UpFileBusiness.GetFileName(hdImageId.Text);
                    HyperLink1.ImageID = hdImageId.Text;
                }
            }
        }
        #endregion

        #region Events

        private void SaveData()
        {
            string id = GetQueryIntValue("id");
            string imageUrl = string.Empty;
            PartyArticle article;

            if (id != "")
            {
                //更新
                article = DB.PartyArticles.Find(id);
                if (article == null)
                {
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                DB.SaveChanges();
            }
            else
            {
                article = new PartyArticle();
                article.ID = StringHelper.NewID;
                article.CreateTime = DateTime.Now;
                article.CreateBy = CurrentUser.LoginID;
                article.Status = 4;

                DB.PartyArticles.Add(article);
            }

            article.Title = txtTitle.Text.Trim();
            article.Subhead = txtSummary.Text.Trim();
            article.Author = txtAuthor.Text.Trim();
            article.AuthorUrl = txtAuthorUrl.Text.Trim();
            article.Module = SysEnum.ArticleModule.ThreeAffairOpen.ToString();
            article.ArticlesLable = txtArticlesLable.Text.Trim();
            article.Summary = txtSummary.Text.Trim();
            article.Flags = Convert.ToInt32(rblFlags.SelectedValue);
            article.Category = ddlCategory.SelectedValue;
            article.BranchID = ppBranch.Value;
            article.Content = this.Request.Form["editorValue"];
            article.ModifyTime = DateTime.Now;
            article.ModifyBy = CurrentUser.LoginID;

            if (!string.IsNullOrEmpty(hdImageId.Text))
            {
                article.ImageID = hdImageId.Text;
            }

            DB.SaveChanges();
        }

        /// <summary>
        /// 保存并关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveData();

            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

        protected void filePhoto_FileSelected(object sender, EventArgs e)
        {
            var rs = UpFileBusiness.Upload(filePhoto, CurrentUser.LoginID);
            if (rs.IsSuccess)
            {
                hdImageId.Text = rs.FileID;
                HyperLink1.ImageID = rs.FileID;
            }
        }
        #endregion
    }
}